package com.apex.sdk.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;

import com.apex.bluetooth.callback.GeneralCallback;
import com.apex.bluetooth.callback.GoalCallback;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.enumeration.QueryWatchInfoType;
import com.apex.bluetooth.model.EABleDailyGoal;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityDailyObjectivesBinding;
import com.apex.sdk.dialog.MonthDialog;
import com.apex.sdk.dialog.WaitingDialog;


public class DailyObjectivesActivity extends AppCompatActivity {
    private WaitingDialog waitingDialog;
    private MonthDialog monthDialog, calorieDialog, distanceDialog, durationDialog;
    private int tempSteps, calorie, distance, duration;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                EABleDailyGoal eaBleDailyGoal = (EABleDailyGoal) msg.obj;
                EABleDailyGoal.EABleDaily stepDaily = eaBleDailyGoal.getS_step();
                EABleDailyGoal.EABleDaily calorieDaily = eaBleDailyGoal.getS_calorie();
                EABleDailyGoal.EABleDaily distanceDaily = eaBleDailyGoal.getS_distance();
                EABleDailyGoal.EABleDaily durationDaily = eaBleDailyGoal.getS_duration();
                EABleDailyGoal.EABleDaily sleepDaily = eaBleDailyGoal.getS_sleep();
                if (stepDaily != null && stepDaily.getGoal() > 0) {
                    tempSteps = stepDaily.getGoal();
                    activityDailyObjectivesBinding.steps.setText(stepDaily.getGoal() + "");
                }
                if (calorieDaily != null && calorieDaily.getGoal() > 0) {
                    calorie = calorieDaily.getGoal();
                    activityDailyObjectivesBinding.calorie.setText(calorieDaily.getGoal() + "");
                }
                if (distanceDaily != null && distanceDaily.getGoal() > 0) {
                    distance = distanceDaily.getGoal();
                    activityDailyObjectivesBinding.distance.setText(distanceDaily.getGoal() + "");
                }
                if (durationDaily != null && durationDaily.getGoal() > 0) {
                    duration = durationDaily.getGoal();
                    activityDailyObjectivesBinding.exerciseDuration.setText(durationDaily.getGoal() + "");
                }
                if (sleepDaily != null && sleepDaily.getGoal() > 0) {
                    activityDailyObjectivesBinding.sleepDuration.setText(sleepDaily.getGoal() + "");
                }
            } else if (msg.what == 0x41) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(DailyObjectivesActivity.this, getString(R.string.failed_to_get_data), Toast.LENGTH_SHORT).show();
            } else if (msg.what == 0x42) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                activityDailyObjectivesBinding.steps.setText(tempSteps + "");
            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(DailyObjectivesActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };
    ActivityDailyObjectivesBinding activityDailyObjectivesBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityDailyObjectivesBinding=ActivityDailyObjectivesBinding.inflate(getLayoutInflater());
        setContentView(activityDailyObjectivesBinding.getRoot());
        activityDailyObjectivesBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityDailyObjectivesBinding.tool);
        getSupportActionBar().setTitle(null);
        activityDailyObjectivesBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tempSteps = 1;
        calorie = 1;
        distance = 1;
        duration = 10;
        EABleConnectState state = EABleManager.getInstance().getDeviceConnectState();
        if (state == EABleConnectState.STATE_CONNECTED) {
            if (waitingDialog == null) {
                waitingDialog = new WaitingDialog(DailyObjectivesActivity.this);
            }
            waitingDialog.show();
            EABleManager.getInstance().queryWatchInfo(QueryWatchInfoType.daily_goal, new GoalCallback() {
                @Override
                public void goalInfo(EABleDailyGoal eaBleDailyGoal) {
                    if (mHandler != null) {
                        Message message = mHandler.obtainMessage();
                        message.what = 0x40;
                        message.obj = eaBleDailyGoal;
                        mHandler.sendMessage(message);
                    }
                }


                @Override
                public void mutualFail(int errorCode) {
                    if (mHandler != null) {
                        mHandler.sendEmptyMessage(0x41);
                    }
                }
            });
        }
        activityDailyObjectivesBinding.steps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monthDialog == null) {
                    monthDialog = new MonthDialog(DailyObjectivesActivity.this, getString(R.string.steps));
                    monthDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            tempSteps = sex;
                            activityDailyObjectivesBinding.steps.setText(sex + "");
                        }
                    });
                    if (!monthDialog.isShowing()) {
                        monthDialog.show();
                    }
                }

            }
        });
        activityDailyObjectivesBinding.calorie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (calorieDialog == null) {
                    calorieDialog = new MonthDialog(DailyObjectivesActivity.this, getString(R.string.calorie));
                    calorieDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            calorie = sex;
                            activityDailyObjectivesBinding.calorie.setText(sex + "");
                        }
                    });
                }
                if (!calorieDialog.isShowing()) {
                    calorieDialog.show();
                }
            }
        });
        activityDailyObjectivesBinding.distance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (distanceDialog == null) {
                    distanceDialog = new MonthDialog(DailyObjectivesActivity.this, getString(R.string.distance));
                    distanceDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            distance = sex;
                            activityDailyObjectivesBinding.distance.setText(sex + "");
                        }
                    });
                }
                if (!distanceDialog.isShowing()) {
                    distanceDialog.show();
                }
            }
        });
        activityDailyObjectivesBinding.exerciseDuration.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (durationDialog == null) {
                    durationDialog = new MonthDialog(DailyObjectivesActivity.this, getString(R.string.exercise_duration));
                    durationDialog.setSelectListener(new MonthDialog.SelectListener() {
                        @Override
                        public void selectData(int sex) {
                            duration = sex;
                            activityDailyObjectivesBinding.exerciseDuration.setText(sex + "");
                        }
                    });
                }
                if (!durationDialog.isShowing()) {
                    durationDialog.show();
                }
            }
        });
        activityDailyObjectivesBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    EABleDailyGoal eaBleDailyGoal = new EABleDailyGoal();
                    EABleDailyGoal.EABleDaily stepsDaily = new EABleDailyGoal.EABleDaily();
                    stepsDaily.setSw(1);
                    stepsDaily.setGoal(tempSteps);
                    EABleDailyGoal.EABleDaily calorieDaily = new EABleDailyGoal.EABleDaily();
                    calorieDaily.setSw(1);
                    calorieDaily.setGoal(calorie);
                    EABleDailyGoal.EABleDaily distanceDaily = new EABleDailyGoal.EABleDaily();
                    distanceDaily.setSw(1);
                    distanceDaily.setGoal(distance);
                    EABleDailyGoal.EABleDaily durationDaily = new EABleDailyGoal.EABleDaily();
                    durationDaily.setGoal(duration);
                    durationDaily.setSw(1);
                    EABleDailyGoal.EABleDaily sleepDaily = new EABleDailyGoal.EABleDaily();
                    sleepDaily.setSw(1);
                    sleepDaily.setGoal(60 * 60 * 8);
                    eaBleDailyGoal.setS_calorie(calorieDaily);
                    eaBleDailyGoal.setS_step(stepsDaily);
                    eaBleDailyGoal.setS_sleep(sleepDaily);
                    eaBleDailyGoal.setS_distance(distanceDaily);
                    eaBleDailyGoal.setS_duration(durationDaily);
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DailyObjectivesActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    EABleManager.getInstance().setDailyGoal(eaBleDailyGoal, new GeneralCallback() {
                        @Override
                        public void result(boolean success, int reason) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x42);
                            }

                        }

                        @Override
                        public void mutualFail(int errorCode) {
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x43);
                            }
                        }
                    });
                }
            }
        });
    }

    @Override
    protected void onDestroy() {

        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (monthDialog != null) {
            monthDialog.dismiss();
            monthDialog.destroyDialog();
            monthDialog = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (calorieDialog!=null){
            calorieDialog.destroyDialog();
            calorieDialog=null;
        }
        if (distanceDialog!=null){
            distanceDialog.destroyDialog();
            distanceDialog=null;
        }
        if (durationDialog!=null){
            durationDialog.destroyDialog();
            durationDialog=null;
        }
        super.onDestroy();
    }
}
